package com.company.emallbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.emallbackend.Enum.ConstantEnum;
import com.company.emallbackend.entity.Cart;
import com.company.emallbackend.entity.Product;
import com.company.emallbackend.entity.User;
import com.company.emallbackend.entity.dto.CartDTO;
import com.company.emallbackend.entity.dto.UpdateCartDTO;
import com.company.emallbackend.exception.CommonException;
import com.company.emallbackend.mapper.CartMapper;
import com.company.emallbackend.mapper.ProductMapper;
import com.company.emallbackend.service.ICartService;
import com.company.emallbackend.utils.TokenUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hanlijian
 * @since 2023-01-12
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
    @Resource
    private CartMapper cartMapper;
    @Resource 
    private ProductMapper productMapper;
    
    @Override
    public void addToCart(List<CartDTO> cartDTOList) {
        User user = TokenUtils.getCurrentAdmin();
        if(Objects.isNull(user)){
            throw new CommonException(201,"请登录后添加购物车信息");
        }
        if(CollectionUtils.isEmpty(cartDTOList)){
            throw new CommonException(201,"请检查您要添加的购物车信息");
        }
        for (CartDTO cartDTO : cartDTOList) {
            Integer productId = cartDTO.getProductId();
            Integer count = cartDTO.getCount(); 
            cartMapper.insert(new Cart(user.getId(),productId,count));
        }
    }

    @Override
    public void updateCart(List<CartDTO> cartDTOList) {
        User user = TokenUtils.getCurrentAdmin();
        if(Objects.isNull(user)){
            throw new CommonException(201,"请登录后修改购物车信息");
        }
        //根据userId删除购物车信息
        cartMapper.delete(new QueryWrapper<Cart>().eq("user_id",user.getId()));
        if(CollectionUtils.isEmpty(cartDTOList)){
            return;
        }
        //添加商品信息到购物车
        this.addToCart(cartDTOList);
    }

    @Override
    public List<Cart> getCartList() {
        User user = TokenUtils.getCurrentAdmin();
        if(Objects.isNull(user)){
            throw new CommonException(201,"请登录后查看购物车信息");
        }
        List<Cart> cartList = cartMapper.selectList(new QueryWrapper<Cart>().eq("user_id", user.getId()));
        List<Cart> carts=new ArrayList<>();
        for (Cart cart : cartList) {
            Integer productId = cart.getProductId();
            //根据productId查询商品信息
            Product product = productMapper.selectById(productId);
            cart.setProduct(product);
            carts.add(cart);
        }
        return carts;
    }

    @Override
    public void deleteCart(Integer cartId) {
        if(Objects.isNull(cartId)){
            throw new CommonException(201,"系统繁忙,稍后再试");
        }
        cartMapper.deleteById(cartId);
    }

    @Override
    public void updateCartStatus(UpdateCartDTO updateCartDTO) {
        Integer cartId = updateCartDTO.getCartId();
        Integer isCheck = updateCartDTO.getIsCheck();
        if(Objects.isNull(isCheck) || Objects.isNull(cartId)){
            throw new CommonException(201,"系统繁忙,稍后再试");
        }
        if(!isCheck.equals(ConstantEnum.NOT_CHECK.getIsCheck()) && !isCheck.equals(ConstantEnum.CHECK.getIsCheck())){
            throw new CommonException(201,"系统繁忙,稍后再试");
        }
        User user = TokenUtils.getCurrentAdmin();
        if(Objects.isNull(user)){
            throw new CommonException(201,"请检查您的登录信息");
        }
        //根据购物车id查询购物车信息
        Cart cart = cartMapper.selectById(cartId);
        if(Objects.isNull(cart)){
            throw new CommonException(201,"系统繁忙,稍后再试");
        }
        Integer dbUserId = cart.getUserId();
        if(!user.getId().equals(dbUserId)){
            throw new CommonException(201,"操作失败");
        }
        cart.setIsCheck(isCheck);
        cartMapper.updateById(cart);
    }

}
